package cmd

import (
	"fmt"
	"wskyygyso/api/pkg/cache"
	"wskyygyso/api/pkg/console"

	"github.com/spf13/cobra"
)

var CmdCache = &cobra.Command{
	Use:   "cache",
	Short: "Cache management",
}

var CmdCachePush = &cobra.Command{
	Use:   "clear",
	Short: "cache clear",
	Args:  cobra.NoArgs,
	Run:   runCacheClear,
}

var CmdCacheForgert = &cobra.Command{
	Use:   "forget",
	Short: "delete redis for redis;example cache forget cache-key",
	Run:   runCacheForget,
}

var cacheKey string

func init() {
	CmdCache.AddCommand(
		CmdCachePush,
		CmdCacheForgert,
	)
	CmdCacheForgert.Flags().StringVarP(&cacheKey, "key", "k", "", "key of the cache")
	CmdCacheForgert.MarkFlagRequired("key")
}

func runCacheClear(cmd *cobra.Command, args []string) {
	cache.Flush()
	console.Success("cache cleared.")
}

func runCacheForget(cmd *cobra.Command, args []string) {
	cache.Forget(cacheKey)
	console.Success(fmt.Sprintf("Cache key [%s] deleted.", cacheKey))
}
